<?php
	function get_gaji($karyawan_id , $kalkulasi_periode){ 
		$query = " SELECT * FROM pr_gaji 
			WHERE periode_id={$kalkulasi_periode}
			AND karyawan_id={$karyawan_id}"
		;
		$result = my_query($query);	
		$datas = my_fetch_array($result);	
		return $datas;
	}
	
	function get_data_list_rapel($komponen_id ,$karyawan_id, $periode_id){
		$query = " SELECT nominal FROM pr_rapel_komponen_karyawan a
			INNER JOIN pr_rapel_periode b ON a.rapel_id = b.rapel_id
			INNER JOIN pr_periode c ON b.periode_bayar = c.tanggal
			WHERE c.periode_id = {$periode_id} AND a.komponen_id = {$komponen_id} AND karyawan_id = {$karyawan_id}";
		$result = my_query($query);
		if(my_num_rows($result) > 0){
			$row = my_fetch_array($result);
			return $row['nominal'];
		}
		return 0;
	}
	
	function get_data_list($karyawan_id , $kalkulasi_periode, $jenis){

		$query = "SELECT a.komponen_id,a.kalkulasi  
				FROM pr_gaji_komponen a 
				LEFT JOIN pr_komponen_gaji b ON a.komponen_id=b.komponen_gaji_id
				WHERE a.periode_id={$kalkulasi_periode} 
				AND a.karyawan_id={$karyawan_id} 
				AND b.tipe={$jenis} 
				AND a.kalkulasi<>0 
				ORDER BY a.komponen_id" ;

		$rs = my_query($query);
		$datas = array();
		while($row = my_fetch_array($rs)){
			$datas[$row['komponen_id']] = $row['kalkulasi'];
		}
		return $datas;
	}

	function ottime($karyawan_id, $periode_id){

		$query = "SELECT SUM(hourpay) AS fdata
			FROM tmn_overtime 
			WHERE karyawan_id ={$karyawan_id} AND out_of_count = 'N'
			AND periode_id={$periode_id}
			LIMIT 1
		";
		
		$res = my_query($query);
		$row = my_fetch_array($res);
		 
		return $row['fdata'];
	}
 
$known_mime_types=array(
 	"pdf" => "application/pdf",
 	"txt" => "text/plain",
 	"html" => "text/html",
 	"htm" => "text/html",
	"exe" => "application/octet-stream",
	"zip" => "application/zip",
	"doc" => "application/msword",
	"xls" => "application/vnd.ms-excel",
	"ppt" => "application/vnd.ms-powerpoint",
	"gif" => "image/gif",
	"png" => "image/png",
	"jpeg"=> "image/jpg",
	"jpg" =>  "image/jpg",
	"php" => "text/plain"
 );
 
 if($mime_type==''){
	 $file_extension = strtolower(substr(strrchr($file,"."),1));
	 if(array_key_exists($file_extension, $known_mime_types)){
		$mime_type=$known_mime_types[$file_extension];
	 } else {
		$mime_type="application/force-download";
	 };
 };
 
 @ob_end_clean();
header('Content-Type: text/plain' );
  header('Content-Disposition: attachment; filename="slip_gaji_'.date('mY').'_'.rand(1000,9999).'.txt"');
 header("Content-Transfer-Encoding: binary");
 header('Accept-Ranges: bytes'); 
 header("Cache-control: private");
 header('Pragma: private');
 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
// echo '<pre>';
 ?> 
<?php
	$periode_gaji_id=get_periode_gaji() ;
	if($periode_id==0){
		$periode_id=$periode_gaji_id;
	}	
	if( $periode_gaji_id==$periode_id ){
		$kalkulasi_periode=0;
	}else{
		$kalkulasi_periode=$periode_id;
	}
	$nik_start = isset($_GET['nik_start']) ? sprintf('%07d',(int)$_GET['nik_start']) :'0000001'; 	
	$nik_end = (isset($_GET['nik_end']) AND (int) $_GET['nik_end'] !='0' )? sprintf('%07d',(int)$_GET['nik_end']) :'9999999'; 	
	$dperiode = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$bulans = array(
		1=>'Januari',
		2=>'Februari',
		3=>'Maret',
		4=>'April',
		5=>'Mei',
		6=>'Juni',
		7=>'Juli',
		8=>'Agustus',
		9=>'September',
		10=>'Oktober',
		11=>'November',
		12=>'Desember',
	);
	$bln = (int)date('m' ,strtotime($dperiode['tanggal']));
	$namabulan = $bulans[$bln]; 	
	$thn = date('Y' ,strtotime($dperiode['tanggal']));
	/*$query = "
		SELECT karyawan_id FROM karyawan a 
		WHERE a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
		AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian)
		AND ( a.nik BETWEEN {$_GET['nik_start']} AND {$_GET['nik_end']} )
		ORDER BY karyawan_id		
	"; */
	$query = "   select a.karyawan_id FROM pr_gaji a INNER JOIN karyawan b ON a.karyawan_id = b.karyawan_id
		 WHERE a.periode_id={$periode_id} AND ( b.nik BETWEEN {$_GET['nik_start']} AND {$_GET['nik_end']} ) ORDER BY b.nik ASC ";
	$result = my_query($query );
	while($row = my_fetch_array($result )):
		$karyawan = loaddata_karyawan($row['karyawan_id']);	
		$jabatan = my_get_data_by_id('jabatan','jabatan_id',(int) $karyawan['jabatan_id']);
		$golongan_jabatan = my_get_data_by_id('golongan_jabatan','golongan_jabatan_id',(int) $jabatan['golongan_jabatan_id']);
		$gajis =  get_gaji($row['karyawan_id'],$kalkulasi_periode);
		$pbi = my_get_data_by_id('pos_biaya','pos_biaya_id',(int) $jabatan['pos_biaya_id']);

?>
 

  <?php echo 'SLIP GAJI  ( '. $namabulan.' '.$thn.' )'; ?> 			         			                      
  NIK      : <?php echo sprintf('%07d',$karyawan['nik']);?>                      T.M.B.  : <?php echo date('d-m-Y',strtotime($karyawan['tmb'])); ?>
  
  Dept/CCC : <?php echo $jabatan['nomor_posisi']; ?> / <?php echo str_pad($pbi['code'],4);?>              St.Kary : <?php echo ucfirst(str_replace( "Karyawan ","",$karyawan['status_karyawan'])); ?>                     
  Nama     : <?php echo str_pad( $karyawan['nama_gelar'] ,29);?>St.Kel. : <?php echo $karyawan['ptkp_status'].($karyawan['ptkp_tanggung']>0? $karyawan['ptkp_tanggung']: '')?>
  
  Jabatan  : <?php 
  echo (strlen($jabatan['nama_jabatan'])<51) ? $jabatan['nama_jabatan']: substr($jabatan['nama_jabatan'],0,47).'...';
  ?>
  
  _______________________________________________________________  

  Standart Gaji : <?php echo $golongan_jabatan['kode_golongan_jabatan']; ?>   Grade : <?php echo str_pad($karyawan['grade_id'],2); ?>        : Rp.<?php echo str_pad( rp_format($gajis['gapok_km']),12," ",STR_PAD_LEFT); ?>

  
<?php echo '      '; ?>A. Gaji Dibayarkan<?php echo '                '; ?>: Rp.<?php echo str_pad( rp_format($gajis['gaji_dibayar']),12," ",STR_PAD_LEFT); ?>

<?php
$text_komponen_rapel_gaji = (int) get_data_list_rapel(0 , $row['karyawan_id'],  $periode_id); 
			if(  $text_komponen_rapel_gaji > 0 ){
			 
			echo 
		'         '.str_pad( 'Rapel ' . substr ( 'gaji' ,0 ,24 ) ,30 ).' : Rp.'.  str_pad( rp_format($text_komponen_rapel_gaji )  ,12," ", STR_PAD_LEFT)."\n"; 
   echo '         ________________________________________________'."\n"; 			
   echo '                                          Rp.' . str_pad(rp_format($gajis['gaji_dibayar'] + $text_komponen_rapel_gaji) , 12," ", STR_PAD_LEFT)."\n" ; 			
			}
?>

<?php echo '      '; ?>B. Tunjangan 
<?php 
		  $komponens = get_data_list($row['karyawan_id'], $kalkulasi_periode , '1');
		  $tunjangan_total  = array_sum($komponens);
		  if(is_array($komponens)){
			foreach($komponens as $komponen_id => $rupiah){
			if($rupiah == 0) continue;
			$kom = my_get_data_by_id('pr_komponen_gaji','komponen_gaji_id',$komponen_id);
			if(strtolower($kom['nama_komponen_gaji'])=='overtime')
			echo 
		'         '.str_pad( substr('Overtime '.ottime($row['karyawan_id'], $periode_id). ' jam',0,30) ,30).' : Rp.'.  str_pad( rp_format($rupiah)  ,12," ", STR_PAD_LEFT)."\n"; 
			else
			echo 
		'         '.str_pad( substr($kom['nama_komponen_gaji'],0,30) ,30).' : Rp.'.  str_pad( rp_format($rupiah)  ,12," ", STR_PAD_LEFT)."\n"; 
			$text_komponen = get_data_list_rapel($komponen_id , $row['karyawan_id'],  $periode_id); 
			if(  $text_komponen > 0 ){
			$tunjangan_total += $text_komponen;
			echo 
		'         '.str_pad( 'Rapel ' . substr ( $kom['nama_komponen_gaji'] ,0 ,24 ) ,30 ).' : Rp.'.  str_pad( rp_format($text_komponen)  ,12," ", STR_PAD_LEFT)."\n"; 
			
			}
		  }}?>
<?php echo '         '; ?>________________________________________________
<?php echo '         '; ?>Total Tunjangan                  Rp.<?php echo str_pad( rp_format($tunjangan_total),12," ",STR_PAD_LEFT); ?>


<?php echo '      '; ?>C. Potongan  
<?php 
		  $komponens = get_data_list($row['karyawan_id'], $kalkulasi_periode , '2');
		  $potongan_total = array_sum($komponens)*-1;
		  if(is_array($komponens)){
			foreach($komponens as $komponen_id => $rupiah){
			if($rupiah == 0) continue;
			$kom = my_get_data_by_id('pr_komponen_gaji','komponen_gaji_id',$komponen_id);
			echo 
		'         '.str_pad( substr($kom['nama_komponen_gaji'],0,30) ,30).' : Rp.'.  str_pad( rp_format($rupiah*(-1))  ,12," ", STR_PAD_LEFT)."\n"; 
			$text_komponen = get_data_list_rapel($komponen_id , $row['karyawan_id'],  $periode_id); 
			if(  $text_komponen > 0 ){
			$potongan_total += $text_komponen ;
			echo 
		'         '.str_pad( 'Rapel ' . substr ( $kom['nama_komponen_gaji'] ,0 ,24 ) ,30 ).' : Rp.'.  str_pad( rp_format($text_komponen )  ,12," ", STR_PAD_LEFT)."\n"; 
			
			}
		  }}?>
<?php echo '         '; ?>________________________________________________
<?php echo '         '; ?>Total Potongan                   Rp.<?php echo str_pad( rp_format($potongan_total ),12," ",STR_PAD_LEFT); ?>


<?php echo '      '; ?>D. Tunjangan Perusahaan
<?php 
		  $komponens = get_data_list($row['karyawan_id'], $kalkulasi_periode  , '3');
		  $tp_total = array_sum($komponens);
		  if(is_array($komponens)){
			foreach($komponens as $komponen_id => $rupiah){
			if($rupiah == 0) continue;
			$kom = my_get_data_by_id('pr_komponen_gaji','komponen_gaji_id',$komponen_id);
			echo 
		'         '.str_pad( substr($kom['nama_komponen_gaji'],0,30) ,30).' : Rp.'.  str_pad( rp_format($rupiah)  ,12," ", STR_PAD_LEFT)."\n"; 
			$text_komponen = get_data_list_rapel($komponen_id , $row['karyawan_id'], $periode_id); 
			if(  $text_komponen > 0 ){
			$tp_total +=  $text_komponen;
			echo 
		'         '.str_pad( 'Rapel ' . substr ( $kom['nama_komponen_gaji'] ,0 ,24 ) ,30 ).' : Rp.'.  str_pad( rp_format($text_komponen)  ,12," ", STR_PAD_LEFT)."\n"; 
			
			}
		  }}?>
<?php echo '         '; ?>________________________________________________
<?php echo '         '; ?>Total Tunj. Perusahaan           Rp.<?php echo str_pad( rp_format($tp_total),12," ",STR_PAD_LEFT); ?>

		  
<?php echo '      '; ?>E. Diterima  ( A + B - C )        : Rp.<?php echo str_pad( rp_format( $gajis['gaji_dibayar'] + $tunjangan_total - $potongan_total),12," ",STR_PAD_LEFT); ?>


<?php echo '      '; ?>HRIS Krakatau Medika (c) 2011 - <?php echo date('Y'); ?> 

<?php echo '      '. get_print_label_value('MSG_GAJI_BARIS_SATU'); ?>                         
<?php echo '      '. get_print_label_value('MSG_GAJI_BARIS_DUA'); ?>                         
<?php echo '      '. get_print_label_value('MSG_GAJI_BARIS_TIGA'); ?>   
<?php echo chr(12); 
 
endwhile;?>